export function pageback() {
	uni.navigateBack({
		delta: 1
	})
}

export function pageto(str, params) {
	if (!!params) {
		uni.switchTab({
			url: str
		})
	} else {
		uni.navigateTo({
			url: str
		})
	}
}

export function showToast(title="", duration=2000, mask=true, icon="none") {
	if (!title) {
		console.log("showToast title is wrong");
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

export const fetch = async (url, data = {}, method = "GET") => {
    const [error, res] = await uni.request({
    	url,
		data,
		method
    })
	if (error) {
		return error;
	} else {
		return res;
	}
}

export function diff_txt(time, lang) {
	let chs = {
		lang1: '今天',
		lang2: '昨天',
		lang3: '前天',
	}
	let en = {
		lang1: 'Today ',
		lang2: 'Yesterday ',
		lang3: 'The day before yesterday ',
	}
	let _lang = lang == "chs" ? chs : en
	if (!time) return {};
            let time1 = (new Date(time)).getDate();
			let time2 = (new Date()).getDate();
			if ((new Date()).getFullYear() == (new Date(time)).getFullYear() && (new Date()).getMonth() == (new Date(time)).getMonth()) {
				if (time1 == time2) {
					return _lang.lang1 + time.slice(11);
				} else if (time2 - time1 == 1) {
					return _lang.lang2 + time.slice(11);
				} else if (time2 - time1 == 2) {
					return _lang.lang3 + time.slice(11);
				} else {
					return time;
				}
			}
			return time;
        }
		
export function _updataTabBar(arr, lang) {
			uni.setTabBarItem({
				index:  0,
				text: arr[lang][0]
			})
			uni.setTabBarItem({
				index:  1,
				text: arr[lang][1]
			})
			uni.setTabBarItem({
				index:  2,
				text: arr[lang][2]
			})
			uni.setTabBarItem({
				index:  3,
				text: arr[lang][3]
			})
		}
		
export 	function checkLogin(logininfo, url) {
			if (!logininfo.id) {
				redirectologin()
				return Promise.resolve(false)
			}
			let _data = {
				id: logininfo.id,
				token: logininfo.token
			}
			return new Promise((resolve, reject) => {
				fetch(url + '/api/index/index2', _data, "post")
					.then(res => {
						if (res.statusCode == 200 && res.data.code == 1) {
							resolve(true)
						} else {
							redirectologin()
						}
					})
					.catch(err => {
						console.log(err)
						redirectologin()
					})
			})
		}
export	function redirectologin() {
		uni.redirectTo({
			url: "/pages/new-login/new-login"
		})
	}

export function forceUpdate (lang) {
	// if (!lang) {
	// 	redirectologin()
	// 	return 
	// }
	let chs = {
		update: "更新提示",
		update_tip: "DC-token 已上架新版本, 您是否更新？",
		isJumping: "正在跳轉...",
		soon_update: "請盡快更新最新版本...",
		fail: "失敗"
	}
	let en = {
		update: "Update prompt",
		update_tip: "The DC-token has been released in a new version. Are you updating?",
		isJumping: "Jumping...",
		soon_update: "Please update the latest version as soon as possible...",
		fail: "Fail",
	} 
	let target = lang == 'chs' ? chs : en
	 // return;
	// #ifdef APP-PLUS  
		// var server = "http://175.126.168.132/api/index/ve"; //检查更新地址
		var server = "http://dc.vipdtc.com/api/index/ve"; //检查更新地   http://27.102.127.203
		var req = { //升级检测数据  
			"appid": plus.runtime.appid,  
			"version": plus.runtime.version
		};
		let _this = this;
		uni.request({  
			url: server,
			data: '',  
			success: (res) => {
				// return;
				// console.log(res)
				if(res.statusCode == 200 && res.data.code == 1){
					let downs = res.data.data.down;
					if(res.data.data.is_up  == '1'){
						if(req.version  == res.data.data.ve){
						}else{
							uni.showModal({ //提醒用户更新  
								title: target.update,  
								content: target.update_tip,
								success: (res) => {  
									if (res.confirm) {
										uni.showToast(target.isJumping)
										setTimeout(()=>{
											plus.runtime.openURL(downs);
										}, 500)												 
									}else{
										uni.showToast(target.soon_update);
										setTimeout(() => {
											// #ifdef APP-PLUS  
											plus.runtime.quit();  
											// #endif
											redirectologin()
										}, 500)
									}
								},
								fail:(err)=>{
									showToast(target.fail)
								}
							})
						}
					}
				} else {
					redirectologin()
				}
			}  
		})  
	//#endif
}